package movEat.Kernel.Import;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;


import movEat.Definitions.MovieDB;
import movEat.Definitions.TVshowDB;
import movEat.Generic.ConfigInfo;
import movEat.Generic.Error;
import movEat.Generic.Progress;
import movEat.IGui.IGui;
import movEat.Kernel.Kernel;
import movEat.Kernel.DataBase.DataBaseManaging;



public class Importer {

	public static void Import(ConfigInfo configInfo){
		List<String> keywords=new LinkedList<String>();
		String[] words=configInfo.getKeyWords();
		for (int i=0;i<words.length;i++){
			keywords.add(words[i]);
		}
		
		
		IGui gui=Kernel.getGui();
		gui.updateBegin();
		Progress prog=new Progress("Going over the new files", 0);
		gui.SetProgress(prog);

		String path=configInfo.getPathOfDBFiles();
		String extension= configInfo.getExtension();
		String fileName= "";
    	ImdbParser parser= new ImdbParser();

    	Map<String, MovieDB> movieMap;
    	
		try {
			fileName= "movies";
			movieMap = parser.parseMoviesTable(path+"movies"+extension);
			
	    	List<String> actors=new ArrayList<String>();
	    	fileName= "actors";
	    	parser.parseActorsTable(path+"actors"+extension, movieMap,actors,false);
	    	int num_of_actors=actors.size();
	    	
	    	fileName= "actresses";
	    	parser.parseActorsTable(path+"actresses"+extension, movieMap,actors,true);
	    	
	    	fileName= "ratings";
	    	parser.parseStarsTable(path+"ratings"+extension, movieMap);

	    	fileName= "countries";
	    	parser.parseCountryTable(path+"countries"+extension, movieMap);
	    	
	    	fileName= "genres";
	    	parser.parseGenreTable(path+"genres"+extension, movieMap);
	    	
	    	fileName= "mpaa-ratings-reasons";
	    	parser.parseRatingTable(path+"mpaa-ratings-reasons"+extension, movieMap);
	    	
	    	fileName= "keywords";
	    	parser.parseKWTable(path+"keywords"+extension, movieMap,keywords);
	    	
	    	fileName= "movies";
	    	Map<String,TVshowDB> seriesMap= parser.parseSeriesTable(path+"movies"+extension);
	    	
	    	fileName= "ratings";
	    	parser.parseStarsSeriesTable(path+"ratings"+extension, seriesMap);
	    	
	    	prog.setProgress(7);
	    	prog.setState("Finding new movies");
			gui.SetProgress(prog);
	    	
			regularImport(movieMap,actors,seriesMap,num_of_actors);
	    	//firstImport(movieMap,actors,seriesMap,num_of_actors);
		} catch (SQLException e) {
    		gui.SetError(new Error (true,"MovEat encountered sql connection difficulties" +
    				"\nconsider checking your username and password" +
    				"\nor the connection properties"));
    		
		} catch (FileNotFoundException e) {
			gui.SetError(new Error (false,"File " +path+fileName+extension +" was not found"));
		} catch (IOException e) {
			gui.SetError(new Error (true,"MovEat encountered difficulties in reading file " 
    				+path+fileName+extension));
		}
	}
		
	static void regularImport(Map<String,MovieDB> movies, List<String> actors,Map<String,TVshowDB> shows,int first_actress) throws SQLException{
		String movie_name,show_name;
		List<String> new_actors;
		int new_actors_first_actress;

		ResultSet rs=DataBaseManaging.getFullTable("movies","imdb_movie_name");
		
		while (rs.next()){
			movie_name=rs.getString("imdb_movie_name");
			if (movies.containsKey(movie_name))
				movies.remove(movie_name);
		}
		
		IGui gui=Kernel.getGui();
		gui.SetMessage("Found "+movies.size()+" new movies.");
		
		Progress prog=new Progress("Finding new actors", 10);
		gui.SetProgress(prog);
		
		new_actors=DataBaseManaging.newActors(actors,first_actress);
		new_actors_first_actress=new_actors.size();
		prog.setState("Finding new actresses");
		prog.setProgress(25);
		gui.SetProgress(prog);
		DataBaseManaging.newActresses(actors,new_actors,first_actress);
		
		gui.SetMessage("Found "+new_actors.size()+" new actors and actresses.");
		
		prog.setState("Entering movies and actors to the DataBase");
		prog.setProgress(40);
		gui.SetProgress(prog);

		//adding all the information into the DB
		DataBaseManaging.addToActorsTable(new_actors,new_actors_first_actress);
		
		DataBaseManaging.addToMoviesTable(movies);
		
		DataBaseManaging.addToActorsMoviesTable(movies,actors,first_actress);
		
		prog.setProgress(97);
		prog.setState("Entering the TV shows");
		gui.SetProgress(prog);
		
		DataBaseManaging.addToDrinksTable(movies);
		
		DataBaseManaging.addToGenresMoviesTable(movies);
		
		rs=DataBaseManaging.getFullTable("tv_shows","tv_imdb_name");
		
		while (rs.next()){
			show_name=rs.getString("tv_imdb_name");
			for (Entry<String, TVshowDB> show : shows.entrySet()){
				if (show_name.equalsIgnoreCase(show.getValue().getImdbName())){
					shows.remove(show.getKey());
					break;
				}
			}
		}
		
		gui.SetMessage("Found "+shows.size()+" new TV shows.");
		
		DataBaseManaging.addToTVshowsTable(shows);		
		
		prog.setProgress(100);
		prog.setState("Done");
		gui.SetProgress(prog);
	
	}
	
	//supports only adding new actors and new movies 
		static void firstImport(Map<String,MovieDB> movies, List<String> actors,Map<String,TVshowDB> shows,int first_actress) throws SQLException{

			//adding all the information into the DB
			System.out.println("adding actors to DB. number of actors = "+actors.size());			
			Map<String,Integer> actors_map=DataBaseManaging.addToActorsTable2(actors,first_actress);;
			
			System.out.println("adding movies to DB. number of movies = "+movies.size());
			DataBaseManaging.addToMoviesTable(movies);
			
			System.out.println("making connections actors-movies");
			DataBaseManaging.addToActorsMoviesTable2(movies,actors,first_actress,actors_map);
			
			System.out.println("add to drinks table");
			DataBaseManaging.addToDrinksTable(movies);
			
			System.out.println("making connections genres-movies");
			DataBaseManaging.addToGenresMoviesTable(movies);
			
			System.out.println("add to tvshows table");
			DataBaseManaging.addToTVshowsTable(shows);		
		
		System.out.println("done!");
		}
	
		
		
	
	


}

